﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using PlayDeezGames.Common;
using System.Xml.Linq;
using DungeonDelver.Engine.Names;

namespace DungeonDelver.Engine
{
    public class Game : DescriptoredEntityBase
    {
        public class Factory : IEntityFactory
        {
            public IEntity LoadFromXElement(IEntity theParent, XElement theXElement)
            {
                return new Game(theXElement);
            }

            public IEntity Create(IEntity theParent)
            {
                return new Game();
            }

            private Factory() { }

            public static void Register()
            {
                EntityFactories.RegisterFactory(Names.EngineFactories.GameFactory, new Game.Factory());
            }
            public static void Unregister()
            {
                EntityFactories.UnregisterFactory(Names.EngineFactories.GameFactory);
            }
        }

        private Atlas atlas = null;

        public Atlas Atlas
        {
            get
            {
                return atlas;
            }
        }

        private Configuration configuration = null;

        public Configuration Configuration
        {
            get
            {
                return configuration;
            }
        }

        private Game()
            : base(null)
        {
            configuration = Configuration.Create(this);
            atlas = Atlas.Create(this);
        }

        private Game(XElement theXElement)
            : base(null, theXElement)
        {
        }


        protected override XElement OnToXElement()
        {
            XElement result = new XElement(Names.EngineXNames.Game);
            result.Add(new XAttribute(PlayDeezGames.Common.Names.CommonXNames.Factory, Names.EngineFactories.GameFactory));
            return result;
        }

        protected override void OnFromXElement(XElement theXElement)
        {
            base.OnFromXElement(theXElement);
            foreach (var child in Children)
            {
                if (child is Atlas)
                {
                    atlas = child as Atlas;
                }
                else if (child is Configuration)
                {
                    configuration = child as Configuration;
                }
            }
        }

        public static Game Create()
        {
            return EntityFactories.Create(null, EngineFactories.GameFactory) as Game;
        }
        public static Game Load(XElement theXElement)
        {
            return EntityFactories.Load(null, theXElement) as Game;
        }
    }
}
